package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.domain.vo.VMVo;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-25
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private IVmTypeService vmTypeService;

    @Autowired
    private INodeService nodeService;

    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    @Override
    public VMVo selectVMVoById(Long id) {
        return vendingMachineMapper.selectVMVoById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine) {
        vendingMachine.setCreateTime(DateUtils.getNowDate());//创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());//修改时间
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        //添加设备的设备编码
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        //添加设备容量
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //添加上次补货时间
//        vendingMachine.setLastSupplyTime(DateUtils.getNowDate());
        //添加详细地址
        vendingMachine.setAddr(node.getAddress());
        //添加商圈类型
        vendingMachine.setBusinessType(node.getBusinessType());
        //添加区域id
        vendingMachine.setRegionId(node.getRegionId());
        //添加合作商id
        vendingMachine.setPartnerId(node.getPartnerId());
        //添加设备状态为默认的未投放状态0
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //新增对应的货道信息
        //存放货道信息
        ArrayList<Channel> channelList = new ArrayList<>();
        //使用双重for循环添加货道信息
        for (int i = 0; i < vmType.getVmRow(); i++) { // 外层为行
            for (int j = 0; j < vmType.getVmCol(); j++) { // 内层为列
                Channel channel = new Channel();
                channel.setChannelCode(vmType.getVmRow() + "-" + vmType.getVmCol());
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        //循环结束后批量添加货道信息
        channelService.batchInsertChannels(channelList);

        return result;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVendingMachine(VendingMachine vendingMachine) {
        if (vendingMachine.getNodeId() != null){
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            //根据点位id重新设置设备的详细地址
            vendingMachine.setAddr(node.getAddress());
            //根据点位id重新设置合作商
            vendingMachine.setPartnerId(node.getPartnerId());
            // 根据点位id重新设置所属区域region
            vendingMachine.setRegionId(node.getRegionId());
            // 根据点位重新设置区域类型
            vendingMachine.setBusinessType(node.getBusinessType());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine
     * @return
     */
    @Override
    public List<VMVo> selectVMVoList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVMVoList(vendingMachine);
    }
}
